package cvc.framework.controller;


import cvc.framework.entity.WebSocketMessage;
import cvc.framework.result.RestMessage;
import cvc.framework.result.RestResult;
import cvc.framework.service.DevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Devices")
public class DevicesController {

    @Autowired
    DevicesService devicesService;

    //获取所有设备信息
    @RequestMapping("/getDevices")
    public RestResult getDevices(String targetName, String uuid) {
        RestResult res = new RestResult();
        res.message = new RestMessage();
        res.message.data = devicesService.getDevices(targetName, uuid);
        return res;
    }

    //通过id获取设备信息
    @RequestMapping("/getDeviceById")
    public RestResult getDeviceById(String targetName, String uuid, String id) {
        RestResult res = new RestResult();
        res.message = new RestMessage();
        res.message.data = devicesService.getDeviceById(targetName, id, uuid);
        return res;
    }

    //添加设备信息
    @PostMapping(value = "/addDevice")
    public RestResult addDevice(@RequestBody WebSocketMessage message) {
        System.out.println(message.toString());
        RestResult res = new RestResult();
        res.message = new RestMessage();
        res.message.data = devicesService.addDevice(message);
        return res;
    }

    //修改设备信息
    @PostMapping(value = "/updateDevice")
    public RestResult updateDevice(@RequestBody WebSocketMessage message) {
        System.out.println(message.toString());
        RestResult res = new RestResult();
        res.message = new RestMessage();
        res.message.data = devicesService.updateDevice(message);
        return res;
    }

    //删除设备信息
    @RequestMapping("/deleteDevice")
    public RestResult deleteDevice(String targetName, String uuid, String id) {
        RestResult res = new RestResult();
        res.message = new RestMessage();
        res.message.data = devicesService.deleteDevice(targetName, uuid, id);
        return res;
    }

    //获取子设备参数
    @RequestMapping("/getParamNames")
    public RestResult getParamNames(String targetName, String uuid, String id){
        RestResult res = new RestResult();
        res.message = new RestMessage();
        res.message.data = devicesService.getParamNames(targetName, uuid, id);
        return res;
    }

    //修改子设备参数
    @PostMapping(value = "/modifyParams")
    public RestResult modifyParams(@RequestBody WebSocketMessage message) {
        System.out.println(message.toString());
        RestResult res = new RestResult();
        res.message = new RestMessage();
        res.message.data = devicesService.modifyParams(message);
        return res;
    }
}
